//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractReview;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class ContractReviewDao extends BaseDao<ContractReview, Integer> {
	public ContractReviewDao() {
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd, String type) {
		ArrayList params = new ArrayList();
		StringBuilder sql = new StringBuilder();
		String sql1;
		if(type == null || type.length() == 0 || "1".equals(type)) {
			sql1 = cd.toSql(params);
			sql.append("select saleId, shopCode, saleDate, saleType, \'1\' type, contractCode, cardType, \'\' ptCode, beginTime, endTime, memberCode, factPrice, earnestAmount, payAmount, reviewStatus, reviewSubmitDate, reviewDate, reviewNote, reviewUser, commissionPeriod, salesCode, salesCode2, verifyMobile, verifyStatus, managerCode\nfrom MemberContract" + (sql1.length() > 0?" where " + sql1:""));
		}

		if(type == null || type.length() == 0 || "2".equals(type)) {
			if(sql.length() > 0) {
				sql.append(" union all ");
			}

			sql1 = cd.toSql(params);
			sql.append("select saleId, shopCode, saleDate, saleType, \'2\' type, contractCode, \'\' cardType, ptCode, beginTime, endTime, memberCode, factPrice, earnestAmount, payAmount, reviewStatus, reviewSubmitDate, reviewDate, reviewNote, reviewUser, commissionPeriod, salesCode, salesCode2, \'\' verifyMobile, 0 verifyStatus, \'\' managerCode\nfrom PtContract" + (sql1.length() > 0?" where " + sql1:""));
		}

		if(sql.length() > 0) {
			sql1 = "select c.saleId, c.shopCode, c.saleDate, c.saleType, c.type, c.contractCode, cardType, ptCode, c.beginTime, c.endTime, c.memberCode, i.chName memberName, factPrice, earnestAmount, c.payAmount\n, reviewStatus, reviewSubmitDate, reviewDate, reviewNote, reviewUser, commissionPeriod, c.salesCode, c.salesCode2, verifyMobile, verifyStatus, managerCode\nfrom (" + sql.toString() + ") c inner join MemberInfo i on c.memberCode=i.memberCode\n" + " order by c.saleDate desc";
			this.queryPageListBySql(ContractInfo.class, pg, sql1, params.toArray());
		}

	}

	public List<ContractReview> getListBySaleId(String saleType, int saleId) {
		return this.queryList("saleType=:p1 and saleId=:p2", new Object[]{saleType, Integer.valueOf(saleId)}, (new OrderSet()).asc("autoId"), -1);
	}

	public void saveReview(ContractReview review) {
		this.add(review);
		String sql;
		if(review.getReviewType().intValue() == 3) {
			if("1".equals(review.getSaleType())) {
				sql = "update MemberContract set reviewStatus=:p1,reviewSubmitDate=getdate(),reviewNote=:p2,reviewUser=:p3 where saleId=:p4";
			} else {
				if(!"2".equals(review.getSaleType())) {
					return;
				}

				sql = "update PtContract set reviewStatus=:p1,reviewSubmitDate=getdate(),reviewNote=:p2,reviewUser=:p3 where saleId=:p4";
			}
		} else if("1".equals(review.getSaleType())) {
			sql = "update MemberContract set reviewStatus=:p1,reviewDate=getdate(),reviewNote=:p2,reviewUser=:p3 where saleId=:p4";
		} else {
			if(!"2".equals(review.getSaleType())) {
				return;
			}

			sql = "update PtContract set reviewStatus=:p1,reviewDate=getdate(),reviewNote=:p2,reviewUser=:p3 where saleId=:p4";
		}

		this.updateBySql(sql, new Object[]{review.getReviewResult(), review.getReviewNote(), review.getReviewUser(), review.getSaleId()});
	}
}
